// ui  layer
var g_uiLayer = null;
var UILayer = cc.Node.extend({
    _money : 0,
    _moneyLabel : null,
    ///////////////////
    _pause : null,
    _resume : null,
    _pauseCad : null,
    ctor:function () {
        this._super();
        g_uiLayer = this;
        g_uiLayer.retain();
        this.initMoney();
        return true;
    },
    onEnter : function(){
        this._super();
        this.addUiBar();
        this.addMoneyLabel();
        this.addPauseMenu();
        this.addPauseCad();
    },
    onExit : function(){
        this._super();
    },
    initMoney : function(){
        var mapObject=GetMapObject();
        this._money=mapObject.money;
    },
    /////////////////////////////////////
    addUiBar : function(){
        var filename = MainResource("menu.png");
        var menuBar = new cc.Sprite( filename );
        menuBar.setPosition( cc.p( cc.winSize.width*0.5,cc.winSize.height-28 ) );
        this.addChild(menuBar);
    },
    addMoneyLabel : function(){
        var filename = MainResource("num.png");
        this._moneyLabel = cc.LabelAtlas.create(this._money, filename,26, 35, '0');
        this._moneyLabel.setPosition( cc.p( 150, cc.winSize.height-43 ) );
        this.addChild(this._moneyLabel);
    },
    addPauseMenu:function(){
        var filename = MainResource("mResume.png");
        this._resume = new cc.MenuItemImage(filename,filename,this.onResume , this);
        this._resume.setPosition( cc.p( cc.winSize.width - 150, cc.winSize.height-28 ) );
        this._resume.setVisible(false);
        filename = MainResource("mPuase.png");
        this._pause = new cc.MenuItemImage(filename,filename,this.onPause , this);
        this._pause.setPosition( cc.p( cc.winSize.width - 150, cc.winSize.height-28 ) );
        var menu = cc.Menu.create( this._resume,this._pause );
        menu.setPosition(cc.p(0,0));
        this.addChild( menu );
    },
    addPauseCad : function(){
        var filename = MainResource("dPuase.png");
        this._pauseCad = new cc.Sprite( filename );
        this._pauseCad.setPosition( cc.p( cc.winSize.width*0.5+1.5,cc.winSize.height-25 ) );
        this._pauseCad.setVisible(false);
        this.addChild(this._pauseCad);
    },
    //////////////////////////////////
    onPause : function(sender){
        cc.log("pause game");
        this.pauseGame();
    },
    onResume : function(sender){
        cc.log("resume game");
        this.resumeGame();
    },
    /////////////////////////////////////
    pauseGame : function(){
        this._pauseCad.setVisible(true);
        this._resume.setVisible(true);
        this._pause.setVisible(false);
        cc.director.pause();
    },
    resumeGame : function(){
        this._pauseCad.setVisible(false);
        this._resume.setVisible(false);
        this._pause.setVisible(true);
        cc.director.resume();
    },
    /////////////////////////////////////
    addMoney : function(add){
        this._money+=add;
        this._moneyLabel.setString(this._money);
        RefreshTower(this._money);
        RefreshTarget(this._money);
    },
    getMoney : function(){
        return  this._money;
        RefreshTower(this._money);
        RefreshTarget(this._money);
    },
    /////////////////////////////////////
    resetting : function(){
        var mapObject=GetMapObject();
        if( mapObject ){
            this._money=mapObject.money;
            this._moneyLabel.setString(this._money);
            cc.log("resetting ui down");
        }else{
            cc.log("resetting ui fail,invalid map object got by GetMapObject");
        }
    }
});
getUILayerInstance=function(){
    return g_uiLayer;
}
AddUiLayer=function(parent){
    if(parent){
        var layer = new UILayer();
        parent.addChild( layer,10 );
        cc.log("*** add ui layer down");
    }else{
        cc.log("*** invalid parent");
    }
}
RemoveUiLayer=function(clear){
    if(g_uiLayer){
        g_uiLayer.removeFromParent(clear);
        cc.log("*** remove ui layer down, clear: "+clear);
    }else{
        cc.log("g_uiLayer is invalid");
    }
}
ResettingUiLayer=function(){
    if(g_uiLayer){
        g_uiLayer.resetting();
    }else{
        cc.log("resetting ui fail,g_uiLayer invalid");
    }
}